#include <bits/stdc++.h>
#define N 10000007
#define mod 998244353
using namespace std;
typedef long long ll;

int T;
int n, a[N], A, B, C, P;
int pos[N];
ll res[3];

struct Pnt { int fi, se; } p[N][3];

ll pw (ll bs, ll t)
{
    ll rres=1;
    while (t) {
        if (t&1) { rres=rres*bs%mod; }
        bs=bs*bs%mod; t>>=1;
    }
    return rres;
}

void prt (Pnt x)
{
    cout<<x.fi<<" "<<x.se<<endl;
}

int main ()
{
    freopen("guess.in", "r", stdin);
    freopen("guess.out", "w", stdout);

    scanf("%d", &T);
    while (T--) {
        scanf("%d %d %d %d %d %d", &n, &a[0], &A, &B, &C, &P);

        for (int i=1; i<=n; i++) {
            a[i]=((ll)a[i-1]*a[i-1] %P *A %P + (ll)B*a[i-1]%P + C) %P +1;
        }

        sort(a+1, a+n+1); a[0]=0;

        int tot=0;
        for (int i=1; i<=n; i++) {
            if (a[i]!=a[i-1]) { pos[++tot]=i; }
        }
        pos[0]=1; pos[tot+1]=n+1; pos[tot+2]=n+1;

        if (tot==1) { printf("1\n"); continue; }

        for (int i=1; i<=tot; i++) {
            res[0]=pos[i]-pos[i-1];
            res[1]=pos[i+1]-pos[i]; res[1]=res[1]*(res[1]-1);
            res[2]=pos[i+2]-pos[i+1];
            
            if (res[1]) { p[i][1]=(Pnt){ 1, 0 };  }
            else { p[i][1]=(Pnt){ 0, 0 }; }

            p[i][2]=(Pnt){ 0-p[i][1].fi-p[i][0].fi, 1-p[i][1].se-p[i][0].se };
            p[i+1][0]=(Pnt){ 2-p[i][2].fi, 0-p[i][2].se };

            // prt(p[i][0]); prt(p[i][1]); prt(p[i][2]);
            // cout<<endl;
        }

        ll div=-p[tot][2].fi;
        ll ans=p[tot][2].se;
        // cout<<div<<" "<<ans<<endl;
        // cout<<pw(ans, mod-2)<<endl;
        ans=ans*pw(div, mod-2)%mod;

        // cout<<pw(p[tot][2].se, mod-2)<<endl;

        ans=(ans%mod+mod)%mod;

        // for (int i=1; i<=n; i++) { cout<<a[i]<<" "; } cout<<endl;

        printf("0 %lld\n", ans);
    }

    return 0;
}